<?php
namespace App\Http\Controllers\Member;

use Illuminate\Support\Facades\Request;
use App\Models\InboxDialog;
use App\Models\Inbox;
use Illuminate\Support\Facades\DB;
/**
 *
 * @author Joker
 *        
 */
class MessageController extends MemberController
{
    
    public function __construct() 
    {
        parent::__construct();        
    }

    
    /**
     * 列表页面
     */
    public function index()
    {
        $notifys = InboxDialog::where(array('sender_uid'=>$this->uid))
                    ->whereHas('inboxes', function($q){
                        $q->where('user_id', '=', $this->uid)->where('semder_remove','=',0)
                            ->orWhere('receipt','=',$this->uid)->where('recipient_remove','=',0);
                    })->orWhere('recipient_uid','=',$this->uid)
                    ->whereHas ('inboxes', function($q){
                        $q->where('user_id', '=', $this->uid)->where('semder_remove','=',0)
                            ->orWhere('receipt','=',$this->uid)->where('recipient_remove','=',0);
                    })->orderBy('created_at','DESC')->paginate(10);

        foreach ($notifys as $notify)
        {
            $ndata = Inbox::where('dialog_id','=',$notify->id)->orderBy('created_at','DESC')->first();
            if(!empty($ndata))
            {
                $notify->message = $ndata->message;
                if($notify->sender_uid != $this->uid)
                {
                    $notify->userlogo =  userlogo($notify->sender_uid, 'middle');
                    $notify->sender_name = getUserName($notify->sender_uid);
                    $notify->unread = $notify->sender_unread;
                }
                else 
                {
                    $notify->userlogo =  userlogo($notify->recipient_uid, 'middle');
                    $notify->sender_name = getUserName($notify->recipient_uid);
                    $notify->unread = $notify->recipient_unread;
                }
            }
            else 
            {
                unset($notify);
            }
        }
        return view('member.message.message')->with('notifys',$notifys);
    }
    
    /**
     * 未读列表
     * @return \Illuminate\View\$this
     */
    public function noRead()
    {
        $notifys = InboxDialog::where('sender_uid','=',$this->uid)->where('sender_unread','>',0)
                     ->whereHas('inboxes', function($q){
                       $q->where('user_id', '=', $this->uid)->where('semder_remove','=',0)
                           ->orWhere('receipt','=',$this->uid)->where('recipient_remove','=',0);
                     })->orWhere('recipient_uid','=',$this->uid)->where('recipient_unread','>',0)
                     ->whereHas ('inboxes', function($q){
                       $q->where('user_id', '=', $this->uid)->where('semder_remove','=',0)
                           ->orWhere('receipt','=',$this->uid)->where('recipient_remove','=',0);
                     })->orderBy('created_at','DESC')->paginate(10);

        foreach ($notifys as $notify)
        {
            $ndata = Inbox::where('dialog_id','=',$notify->id)->orderBy('created_at','DESC')->first();
            if(!empty($ndata))
            {
                $notify->message = $ndata->message;
                if($notify->sender_uid != $this->uid)
                {
                    $notify->userlogo =  userlogo($notify->sender_uid, 'middle');
                    $notify->sender_name = getUserName($notify->sender_uid);
                    $notify->unread = $notify->sender_unread;
                }
                else 
                {
                    $notify->userlogo =  userlogo($notify->recipient_uid, 'middle');
                    $notify->sender_name = getUserName($notify->recipient_uid);
                    $notify->unread = $notify->recipient_unread;
                }
            }
            else 
            {
                unset($notify);
            }
        }
        return view('member.message.message')->with('notifys',$notifys);
    }
    
    /**
     * 消息详情
     */
    public function show($id)
    {
        if(Request::ajax())
        {
            $result = InboxDialog::with('sender')->with('recipient')
                    ->where(array('id'=>$id,'sender_uid'=>$this->uid))->orWhere(array('id'=>$id,'recipient_uid'=>$this->uid))
                    ->first();
            if(empty($result))
            {
                return $this->formatJson('error','网络异常！');
            }
            $data = Inbox::where(array('dialog_id'=>$result->id,'user_id'=>$this->uid,'semder_remove'=>0))
                         ->orWhere(array('dialog_id'=>$result->id,'receipt'=>$this->uid,'recipient_remove'=>0))
                         ->orderBy('created_at','asc')->get();
            if (empty($data))
            {
                return $this->formatJson('error','没有更多数据！');
            }
            if($result->sender_uid != $this->uid)
            {
               $result->recipient_unread = 0;
               $result->save();
               $result->otherLogo = userlogo($result->sender_uid, 'small');
               $result->rid = $result->sender_uid;
               $result->recipientname = getUserName($result->sender_uid);
            }
            else
            {
                $result->sender_unread = 0;
                $result->save();
                $result->otherLogo = userlogo($result->recipient,'small');
                $result->rid = $result->recipient_uid;
                $result->recipientname = getUserName($result->recipient_uid);
            }
            $result->selfLogo = userlogo($this->uid,'small');
            $result->uid = $this->uid;
            $result->data = $data;
            return $this->formatJson('success','成功',$result);
        }
        else
        {
            abort(404);
        }
    }
    
    /**
     * 用户发送私信
     */
    public function store(\Illuminate\Http\Request $req)
    {
        if($req->ajax())
        {
            $recipient_uid = $req->input('rid',0);
            $notifyid = $req->input('id',0);
            $content = $req->input('content','');
            $notify = InboxDialog::where(array('id'=>$notifyid))->first();
            if(empty($notify))
            {
                return $this->formatJson('error','数据异常！');
            }
            elseif($notify->sender_uid != $this->uid && $notify->recipient_uid != $this->uid)
            {
                return $this->formatJson('error','数据异常！');
            }
            elseif ($notify->sender_uid != $recipient_uid && $notify->recipient_uid != $recipient_uid)
            {
                return $this->formatJson('error','数据异常！');
            }
            if($notify->sender_uid == $this->uid)
            {
                $notify->incrementAdd('sender_unread',1);
            }
            else
            {
                $notify->incrementAdd('recipient_unread',1);
            }
            if(!empty($content))
            {
                $notifyData = new Inbox();
                $notifyData->message = $content;
                $notifyData->dialog_id = $notifyid;
                $notifyData->user_id = $this->uid;
                $notifyData->receipt = $recipient_uid;
                $notifyData->save();
                $notifyData->selfLogo = userlogo($this->uid,'small');
                $notifyData->created_at = date('Y-m-d H:i:s'); 
                return $this->formatJson('success','成功！',$notifyData);
            }
            else 
            {
                return $this->formatJson('error','内容不能为空！');
            }
        }
        else 
        {
            abort(404);
        }
    }


    /**
     * 删除
     */
    public function destroy($id)
    {
        $notifys = InboxDialog::find($id);

        if(empty($notifys))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::transaction(function() use ($notifys){
                Inbox::where(['dialog_id'=>$notifys->id,'user_id' => $this->uid])->update(['semder_remove' => 1]);
                Inbox::where(['dialog_id'=>$notifys->id,'receipt' => $this->uid])->update(['recipient_remove' => 1]);
            });
            return $this->formatJson('success','资源删除成功！');
        }
    }
}